package com.liukai.personalbills.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.liukai.personalbills.R;
import com.liukai.personalbills.bean.UserMessage;
import com.liukai.personalbills.retrofitInterface.UserAPI;
import com.liukai.personalbills.util.RetrofitManager;
import com.liukai.personalbills.util.TextUtil;
import com.liukai.personalbills.util.ToastUtil;
import com.liukai.personalbills.util.ValidateUtil;

import java.net.HttpURLConnection;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText tvUser;
    private EditText tvPass;
    private AppCompatButton btnLogin;
    private AppCompatButton btnRegister;
    private Context context = LoginActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initListener();

    }

    private void initView() {
        tvUser = findViewById(R.id.tvUser);
        tvPass = findViewById(R.id.tvPass);
        btnLogin = findViewById(R.id.btnLogin);
        btnRegister = findViewById(R.id.btnRegister);
    }

    private void initListener(){
        tvUser.setOnClickListener(this);
        tvPass.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.btnLogin:

                String username = tvUser.getText().toString();
                String password = tvPass.getText().toString();
//                if(validate(username,password)){ // 校验用户名密码是否正确
                    getLogin(username,password); // 发送Http请求
//                }

                break;

            case R.id.btnRegister:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
        }

    }

    /**
     * 发送Http请求
     * @param username
     * @param password
     */
    private void getLogin(String username, String password) {

        UserAPI userAPI = RetrofitManager.getUserAPI();
        Call<UserMessage> loginTask = userAPI.login(username, password);
        loginTask.enqueue(new Callback<UserMessage>() {
            @Override
            public void onResponse(Call<UserMessage> call, Response<UserMessage> response) {
                int code = response.code();
                if(code == HttpURLConnection.HTTP_OK){
                    UserMessage body = response.body();

                    ToastUtil.log(context,"onResponse",response.body().toString());
                    String message = body.getMessage();
                    if(message.equals(TextUtil.LOGIN_SUCCESS)){
                        // TODO: 登陆成功
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        TextUtil.username = username;
//                        intent.putExtra("username",username);
                        startActivity(intent);
                    }else if(message.equals(TextUtil.LOGIN_FAIL_ERROR_PASS)){
                        // TODO: 密码错误
                    }else if(message.equals(TextUtil.LOGIN_FAIL_NO_USER)){
                        // TODO: 该用户不存在
                    }else{
                        // TODO: 登陆失败 : 请输入完整的信息
                    }
                    ToastUtil.show(context,message);
                }else{
                    ToastUtil.show(context,"错误:" + code);
                }


            }

            @Override
            public void onFailure(Call<UserMessage> call, Throwable t) {
                ToastUtil.show(context,"请求失败，请检查网络是否连接");
                ToastUtil.log(context,"onFailure",t.toString());
            }
        });

    }

    /**
     * 校验用户名密码是否正确
     * @param user
     * @param pass
     * @return
     */
    private boolean validate(String user, String pass){

        if(ValidateUtil.isPhone(user)){
            if(ValidateUtil.isPassword(pass)){
                return true;
            }else{
                ToastUtil.show(this,"密码长度6至18位，且只能包含数字，字母大小写*.");
            }
        }else{
            ToastUtil.show(this,"请输入有效手机号");
        }
        return false;
    }


}
